﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Web;
using FetCo.Framework;
using Friso.Entities;
using Friso.Data;
using FetCo.Data;
using Friso.Business;
public partial class login1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUsername.Focus();
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {

        if (!Page.IsValid) return;
        if (txtUsername.Value == string.Empty && txtPassword.Value == string.Empty)
        {
            lblMessage.Text = "Nhập tài khoản và mật khẩu !";
            return;
        }
        string username = txtUsername.Value.Trim();
        string password = txtPassword.Value;
        password = Utilitys.Encrypt(password);

        try
        {
            Criteria cri = new Criteria();
            cri.add("[" + Friso.Entities.User._Table + "].[" + UserColumns.Enable + "]", 1, Condition.EQUAL, Condition.AND);
            cri.add("[" + Friso.Entities.User._Table + "].[" + UserColumns.UserName + "]", username, Condition.EQUAL, Condition.AND);
            UserCollection _UserCollection = UserService.GetUserListByParam("[" + Friso.Entities.User._Table + "].[" + UserColumns.UserName + "]", OrderDirection.ASC, cri.Criter);
            Friso.Entities.User _user = new User();
            if (_UserCollection.Count > 0)
            {
                _user = _UserCollection[0];
            }
            if (_user.UserName.Equals(null) || !_user.Password.Trim().Equals(password))
            {
                lblMessage.Text = "Tài khoản và mật khẩu không đúng!";
                return;
            }
            Criteria criuserPermission = new Criteria();
            criuserPermission.add("[" + Friso.Entities.UserPermission._Table + "].[" + UserPermissionColumns.UserId + "]", _user.UserId, Condition.EQUAL, Condition.AND);
            UserPermissionCollection _UserPermissionCollection = UserPermissionService.GetUserPermissionListByParam("[" + Friso.Entities.UserPermission._Table + "].[" + UserPermissionColumns.UserId + "]", OrderDirection.ASC, criuserPermission.Criter);
            Friso.Entities.UserPermission _UserPermission = new UserPermission();
            if (_UserPermissionCollection.Count > 0)
            {
                _UserPermission = _UserPermissionCollection[0];
            }

            Session["role"] = _UserPermission.CategoryId;
            Session["user"] = _user.FullName;
            Session["userid"] = _user.UserId;
            Session["username"] = _user.UserName;
            Criteria criPermission = new Criteria();
            criPermission.add("[" + Friso.Entities.Permission._Table + "].[" + PermissionColumns.PermissionId + "]", _UserPermission.PermissionId, Condition.EQUAL, Condition.AND);
            PermissionCollection _PermissionCollection = PermissionService.GetPermissionListByParam("[" + Friso.Entities.Permission._Table + "].[" + PermissionColumns.PermissionId + "]", OrderDirection.ASC, criPermission.Criter);
            Friso.Entities.Permission _Permission = new Permission();
            if (_PermissionCollection.Count > 0)
            {
                _Permission = _PermissionCollection[0];
            }
            Session["roleName"] = _Permission.PermissionName;
            lblMessage.Text = string.Empty;
        }
        catch (Exception ex)
        {
            Logger.Error(ex);
            lblMessage.Text = ex.Message;
            return;
        }
        FormsAuthenticationHelper.RedirectFromLoginPage(username, new string[0], TimeSpan.FromMinutes(30), "Admin/Default.aspx", false);
    }
}
